package com.joe.blockview.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.joe.blockview.model.Plain;

import java.util.ArrayList;
import java.util.List;

public class SharedPreUtils {
    public static List<Plain> getPlainList(Context context) {
        SharedPreferences sp = context.getSharedPreferences("plain", Context.MODE_PRIVATE);
        String str = sp.getString("list", "");
        Gson gson = new Gson();
        List<Plain> ps = gson.fromJson(str, new TypeToken<List<Plain>>() {
        }.getType());
        return ps == null ? new ArrayList() : ps;
    }

    public static void savePlains(Context context, List<Plain> plains) {
        SharedPreferences sp = context.getSharedPreferences("plain", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        Gson gson = new Gson();
        editor.putString("list", gson.toJson(plains));
        editor.commit();
    }
}
